package com.gip.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gip.pojo.User;
import com.gip.service.UserService;
import com.gip.utils.R;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
@CrossOrigin
@Controller
public class TestController {
    @Autowired
    UserService userService;

    @RequestMapping("/responseData")
    public String responseData() {
        Map map = new HashMap();
//        User user = new User(12, "杨总", "123321", "110", "5552@qq.com", 1);
//        map.put("data", user);
        map.put("code", 0);

        //使用阿里巴巴的FastJSON
        String mapJson = JSON.toJSONString(map);
        System.out.println(mapJson);
        //创建响应对象
//        R r = new R(0, "成功", 1, user,"success");
//        String s = JSON.toJSONString(r);
        return mapJson;
    }
    @RequestMapping("/test1")
    @ResponseBody
    public String test1() {
        List<User> allUser = userService.findAllUser();
        //创建响应对象
        R r = new R(0, "成功", 1,allUser ,"success");
        String s = JSON.toJSONString(r);
        return s;
    }
//    @RequestMapping("/test")
//    @ResponseBody
//    public Map test(int page, int limit, String searchParams) {
////		List<User> list = userService.findAllUser();
//        HashMap<String, Object> map = new HashMap<>();
//        map.put("index", (page - 1) * limit);
//        map.put("limit", limit);
//
//        System.out.println("输出");
//        System.out.println(searchParams);
//        if (searchParams != null) {
//            JSONObject jsonObject = JSON.parseObject(searchParams);
//            String userName = jsonObject.getString("userName");
//            System.out.println(userName);
//            String phone = jsonObject.getString("phone");
//            String email = jsonObject.getString("email");
//            String password = jsonObject.getString("password");
//            map.put("userName", userName);
//            map.put("phone", phone);
//            map.put("email", email);
//            map.put("password", password);
//        }
//        List<User> list = userService.findUserByIf(map);
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("code", 0);
//
//        int i = userService.conutUser();
//        hashMap.put("count", i);
//        hashMap.put("data", list);
//
////		User user = new User(1, "gip", "123", "321", "123", 1);
//        return hashMap;
//    }

//    @RequestMapping("/table")
//    public String table() {
//        return "table";
//    }

//    @RequestMapping("/edit")
//    public String edit(int id, Model model) {
//        User user = userService.findUserById(id);
//        model.addAttribute("user", user);
//        return "table/edit";
//    }

//    @RequestMapping("/hello")
//    @ResponseBody
//    public Map hello(String data) {
//        System.out.println(data);
//        JSONObject jsonObject = JSON.parseObject(data);
//        int id = jsonObject.getInteger("id");
//        String userName = jsonObject.getString("userName");
//        String phone = jsonObject.getString("phone");
//        String email = jsonObject.getString("email");
//        User user = new User();
//        user.setId(id);
//        user.setUserName(userName);
//        user.setPhone(phone);
//        user.setEmail(email);
//        // 调用更新
//        userService.upDateUser(user);
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("status", "success");
//        hashMap.put("msg", "success");
//        return hashMap;
//    }

//    @PostMapping("/delete")
//    @ResponseBody
//    public Map delete(int id) {
//        System.out.println(id);
//        // 调用删除
//        userService.deleteUser(id);
//        HashMap<String, Object> hashMap = new HashMap<>();
//        hashMap.put("status", "success");
//        hashMap.put("msg", "success");
//        return hashMap;
//    }

}
